package com.caojm.mptools.client.events;

import com.caojm.mptools.client.MiningData;  // 引入挖掘数据类
import net.fabricmc.fabric.api.event.player.PlayerBlockBreakEvents;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

public class MiningEventListener {

    // 监听玩家挖掘方块事件
    public void onBlockBreak(PlayerEntity player, World world, BlockPos pos) {
        // 检查玩家是否是 ServerPlayerEntity 类型
        if (player instanceof ServerPlayerEntity) {
            // 更新玩家的挖掘数据
            ServerPlayerEntity serverPlayer = (ServerPlayerEntity) player;
            MiningData.addMiningData(serverPlayer.getName().getString());

            // 输出日志信息
            System.out.println(serverPlayer.getName().getString() + " mined block at " + pos);
        }
    }

    // 注册事件监听
    public static void register() {
        PlayerBlockBreakEvents.BEFORE.register((player, world, pos, state, blockEntity) -> {
            // 触发事件
            new MiningEventListener().onBlockBreak(world, player, pos);
            return true; // 返回 true 继续执行挖掘事件
        });
    }
}
